package com.scrawler.html.template;

import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.htmlparser.NodeFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.springframework.stereotype.Service;

import com.scrawler.main.InitProject;
import com.scrawler.mysql.dao.KeywordSql;
import com.scrawler.mysql.dao.WebconfigSql;
import com.scrawler.vo.Keyword;
import com.scrawler.vo.NewsContentConfig;
import com.scrawler.vo.WebConfig;

/**  
 * @Description: 描述
 * @author kuntang    
 * @version 1.0  
 * @created 2012-10-23 下午6:01:10 
 * Copyright (c) 2012 sohu,Inc. All Rights Reserved.
 */

@Service
public class NBAPageTemplate {

	private static final Log LOG = LogFactory.getLog("nbaChannelLog");
	public static List<WebConfig> configs = WebconfigSql.getWebConfig();
	public static Map<Integer,NewsContentConfig>  newsContentConfig = WebconfigSql.getContentConfig();
	public static List<Keyword> keywords = KeywordSql.getKeywordConfig();
	
	// sina search 
	
	public void run(){
		LOG.info("crawl nba page.......");
		// String keyWord = "NBA";

		//for(Keyword keyword : keywords){
			for(WebConfig config : configs){
				if(config.getWeb_type()!=2)continue;
				// 抓取到指定页数
				for(int i=0;i<config.getCrawlDepth();i++){
					// 提交单个搜索任务到搜索线程池
					HtmlPageTemplate template = InitProject.getContext().getBean(HtmlPageTemplate.class);
					System.out.println("抓取到第 "+i+" 层.....");
					//template.init(config,config.getUrl()+keyword.getKeyword()+"&"+config.getNextPage()+i);
					template.init(config,config.getUrl()+"王林"+"&"+config.getNextPage()+i);
					IndexSearchCrawlerPool.getInstance().addTask(template);
				}				
			//}
			// 更新关键词最后抓取时间
			// KeywordSql.updateLastCrawlTime(keyword.getId());
			
		}
		/** nba 首页   */ 
	}
	
	public NodeFilter[] createFilters(List<String[]> names){
		NodeFilter filters[] = new NodeFilter[names.size()];
		for(int i=0;i<names.size();i++){
			String values[] = names.get(i);
			filters[i] = new HasAttributeFilter(values[0],values[1]);
		}
		return filters;
	}
	
	/**
	 * @Description:
	 * @Date: 2012-10-23
	 * @param: @param args
	 * @return: void
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		InitProject.init();
		NBAPageTemplate nbaPage = InitProject.getContext().getBean(NBAPageTemplate.class);
		nbaPage.run();
	}
	
}
